package threads;

public class Thread1 {
    public static void main(String [] args)
    {
        Thread2 t=new Thread2();
        //这里的线程不是同一个，因为每次的共享变量都不一样
        new Thread(t).start();
        new Thread(t).start();
        new Thread(t).start();
    }

}
class Thread2 implements Runnable
{
     static volatile int tickets=100;//设置共享变量，是线程之间可以进行信息共享，可以进行细粒度信息交流
    String str=new String("");
    public void run()
    {
        while(true)
        {
            synchronized(str) {
                if (tickets > 0) {
                    System.out.println("车票还剩余：" + tickets + " " + "线程是：" + Thread.currentThread().getName());
                    tickets = tickets - 1;
//
                } else {
                    break;
                }
            }
        }
    }
}